import * as THREE from 'three'

export const sun = [
  '/assets/textures/posx.jpg',
  '/assets/textures/negx.jpg',
  '/assets/textures/posy.jpg',
  '/assets/textures/negy.jpg',
  '/assets/textures/posz.jpg',
  '/assets/textures/negz.jpg'
]
export const gray = [
  '/assets/textures/posx_gray.jpg',
  '/assets/textures/negx_gray.jpg',
  '/assets/textures/posy_gray.jpg',
  '/assets/textures/negy_gray.jpg',
  '/assets/textures/posz_gray.jpg',
  '/assets/textures/negz_gray.jpg'
]

export const night = [
  '/assets/textures/night/px.jpg',
  '/assets/textures/night/nx.jpg',
  '/assets/textures/night/py.jpg',
  '/assets/textures/night/ny.jpg',
  '/assets/textures/night/pz.jpg',
  '/assets/textures/night/nz.jpg'
]

/**
 * 场景天空盒
 */
export default class SkyBoxs {
  skybox = ['sun', 'gray', 'night']
  constructor(_viewer) {
    this.viewer = _viewer
  }

  /**
   * 添加雾效果
   */
  addFog(color = 'rgb(9,9,9)', near = 0.01, far = 50) {
    this.viewer.scene.fog = new THREE.Fog(new THREE.Color(color), near, far)
  }

  /**
   * 移除雾效果
   */
  removeFog() {
    this.viewer.scene.fog = null
  }

  /**
   * 添加默认天空盒
   * @param index
   */
  addSkybox(index = 0) {
    const path = `@/assets/skybox/${this.skybox[index]}/` // 设置路径
    const format = '.jpg' // 设定格式
    this.setSkybox(path, format)
  }

  /**
   * 自定义添加天空盒
   * @param path 天空盒地址
   * @param format 图片后缀名
   */
  setSkybox(path, format = '.jpg') {
    const loaderbox = new THREE.CubeTextureLoader()
    const cubeTexture = loaderbox.load([
      path + 'posx' + format,
      path + 'negx' + format,
      path + 'posy' + format,
      path + 'negy' + format,
      path + 'posz' + format,
      path + 'negz' + format
    ])
    // 需要把色彩空间编码改一下，原因我上一篇说过的
    cubeTexture.encoding = THREE.sRGBEncoding
    this.viewer.scene.background = cubeTexture
  }
}
